class NoticePanel extends BasePanel {

    private btnGroup: eui.Group;

    private scroller: eui.Scroller;
    private lab_description: eui.Label;

    private skip: eui.Group;
    private btn_skip: ButtonEx;

    private active: any;

    public constructor() {
        super();

        this.skinName = "NoticePanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.clean();

        let act: any;
        let item:NoticeBtnItem;
        for (let i: number = 0; i < GameConfig.active_list.length; i++) {
            act = GameConfig.active_list[i];

            item = new NoticeBtnItem();
            this.btnGroup.addChild(item);

            item.update(act);

            item.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        }

        this.btn_skip.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent): void {
        switch (e.currentTarget) {
            case this.btn_skip:
                if (this.active && this.active.hasOwnProperty('link')) location.href = this.active.link;
                break;
            default:
                this.change(e.currentTarget.label);
                break;
        }
    }

    private change(title: string): void {

        this.active = null;

        let act: any;
        for (let i: number = 0; i < GameConfig.active_list.length; i++) {
            act = GameConfig.active_list[i];

            if (act.title != title) continue;

            this.active = act;

            this.lab_description.text = "" + this.active.desc;

            this.showSkip = this.active.hasOwnProperty('link') && this.active.link && this.active.link != '';
        }

        let item: NoticeBtnItem;
        for (let i: number = 0; i < this.btnGroup.numElements; i++) {
            item = <NoticeBtnItem>this.btnGroup.getElementAt(i);

            if (item && item.label == this.active.title) {
                item.selected = true;
            }
        }
    }

    show() {
        super.show();

        if (GameConfig.active_to > 0) {
            let act: any;
            for (let i: number = 0; i < GameConfig.active_list.length; i++) {
                act = GameConfig.active_list[i];

                if (act.id != GameConfig.active_to) continue;

                this.change(act.title);
            }
        }
        else {
            if (GameConfig.active_list.length) this.change(GameConfig.active_list[0].title);
        }
    }

    set showSkip(b: boolean) {
        this.skip.visible = b;
        this.scroller.bottom = b ? 88 : 10;
    }

    clean() {
        this.btnGroup.removeChildren();
        this.btnGroup.scrollV = 0;
        this.btnGroup.validateNow();

        this.lab_description.text = '';
    }
}